package com.tj.jinzao.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.utils.ToastUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class BindPhoneActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back_lay)
    LinearLayout back_lay;
    @BindView(R.id.sendSMS)
    TextView sendSMS;
    @BindView(R.id.et_phone)
    EditText phone;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.bt_login)
    Button bt_login;

    private int postion=0;
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;


    public void detachView() {
        onUnsubscribe();
    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 101:
                    postion--;
                    if(postion<=0){
                        sendSMS.setText("发送验证码");
                        return;
                    }
                    sendSMS.setText(String.valueOf(postion)+"s");
                    break;
                case 102:
                    break;
                case 103:
                    break;
            }
        }
    };
    private Timer timer=null;
    private TimerTask timerTask=null;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_bind_phone;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        detachView();
        if(timerTask!=null){
            timerTask.cancel();
            timerTask=null;
        }
        if(timer!=null){
            timer.cancel();
            timer=null;
        }
    }

    @Override
    protected void initLinstener() {
        back_lay.setOnClickListener(this);
        sendSMS.setOnClickListener(this);
        bt_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back_lay:
                finish();
                break;
            case R.id.sendSMS:
                setSendSMS();
                break;
            case R.id.bt_login:
                getSubmit();
                break;
        }
    }
    private void getSubmit(){
//        addSubscription(mApiService.bindPhone(phone.getText().toString().trim(), "bind"), new SubscriberCallBack<Boolean>(BindPhoneActivity.this) {
//            @Override
//            protected void onSuccess(Boolean response) {
//                ToastUtils.showToast(BindPhoneActivity.this,"验证码发送成功");
//                postion=60;
//
//
//            }
//
//            @Override
//            protected void onFailure(ResultResponse response) {
//            }
//
//            @Override
//            protected boolean isShowProgress() {
//                return true;
//            }
//        });

    }
    private void setSendSMS(){
        if(postion<=0){
            if(TextUtils.isEmpty(phone.getText())){
                ToastUtils.showToast(BindPhoneActivity.this,"请输入手机号");
            }else if(Constant.isMobileNO(phone.getText().toString())){
                addSubscription(mApiService.sendCode(phone.getText().toString().trim(), "bind"), new SubscriberCallBack<Boolean>(BindPhoneActivity.this) {
                    @Override
                    protected void onSuccess(Boolean response) {
                        ToastUtils.showToast(BindPhoneActivity.this,"验证码发送成功");
                        postion=60;
                        if(timer==null){
                            timer=new Timer();
                            timerTask=new TimerTask() {
                                @Override
                                public void run() {
                                    handler.sendEmptyMessage(101);
                                }
                            };
                            timer.schedule(timerTask,100,1000);
                        }


                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
            }else {
                ToastUtils.showToast(BindPhoneActivity.this,"手机号格式错误");
            }

        }

    }
}
